/**
 * @author ahammer
 * @date 2009
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tools;

import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.RenderedImage;
import java.awt.image.SampleModel;
import java.io.File;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;

import main.Board;
import main.Look;

public class ImageInfo {
	
public static void getCurrImgInfo(){
	PlanarImage pi = Look.getCurrImg();
	if (pi == null){
		Board.appendTexln("ImageInfo: No image and therefore no information");
		return;
	}
	ImageInfo.printInfo(pi);
}

public static void printInfo(PlanarImage pi) {
	if (pi == null){
		Board.appendTexln("ImageInfo: No image and therefore no information");
		return;
	}
	// Get the image file size (non-JAI related).
	//File image = new File(args[0]);
	//BoardJ.appendTexln("Image file size: "+image.length()+" bytes.");	
	// Show the image dimensions and coordinates.
	Board.appendTexln("Image Name:        " + pi.getProperty("image_name"));
	Board.appendTexln("File Name:         " + pi.getProperty("file_name"));
	Board.appendTex  ("Dimensions:        ");
	Board.appendTex(pi.getWidth()+"x"+pi.getHeight()+" pixels");
	// Remember getMaxX and getMaxY return the coordinate of the next point!
	Board.appendTexln(" (minimum "+pi.getMinX()+","+pi.getMinY()+")"); //+" to ") + (pi.getMaxX()-1)+","+(pi.getMaxY()-1)+")");
	
	Board.appendTexln("Number of Sources: " + pi.getNumSources());
	Board.appendTexln("Tile Factory:      " + pi.getTileFactory());
//	BoardJ.appendTexln("JAI.getDefaultTileSize() "+ JAI.getDefaultTileSize());
//	BoardJ.appendTexln("JAI parallelism:         "+ JAI.getDefaultInstance().getTileScheduler().getParallelism());
//	BoardJ.appendTexln("JAI PrefetchParallelism: "+ JAI.getDefaultInstance().getTileScheduler().getPrefetchParallelism());
//	BoardJ.appendTexln("JAI PrefetchPriority:    "+ JAI.getDefaultInstance().getTileScheduler().getPrefetchPriority());
//	BoardJ.appendTexln("JAI Priority:            "+ JAI.getDefaultInstance().getTileScheduler().getPriority());
	
	if ((pi.getNumXTiles() != 1)||(pi.getNumYTiles() != 1)){ // Is it tiled?
		// Tiles number, dimensions and coordinates.
		Board.appendTex("Tiles:             ");
		Board.appendTex(pi.getTileWidth()+"x"+pi.getTileHeight()+" pixels"+" ("+pi.getNumXTiles()+"x"+pi.getNumYTiles()+" tiles)");
		Board.appendTex(" (minimum "+pi.getMinTileX()+","+pi.getMinTileY()+")");//+" to "+pi.getMaxTileX()+","+pi.getMaxTileY()+")");
		Board.appendTexln(" offset: "+pi.getTileGridXOffset()+","+pi.getTileGridXOffset());
	}
	// Display info about the SampleModel of the image.
	SampleModel sm = pi.getSampleModel();
	int dt = pi.getAsBufferedImage().getType();
	String dataType;
    switch(dt) {
   		case BufferedImage.TYPE_3BYTE_BGR:      dataType= "TYPE_3BYTE_BGR";
  		case BufferedImage.TYPE_4BYTE_ABGR:     dataType= "TYPE_4BYTE_ABGR";
  		case BufferedImage.TYPE_4BYTE_ABGR_PRE: dataType= "TYPE_4BYTE_ABGR_PRE";
  		case BufferedImage.TYPE_BYTE_BINARY:    dataType= "TYPE_BYTE_BINARY";
  		case BufferedImage.TYPE_BYTE_GRAY:      dataType= "TYPE_BYTE_GRAY";
  		case BufferedImage.TYPE_BYTE_INDEXED:   dataType= "TYPE_BYTE_INDEXED";
  		case BufferedImage.TYPE_CUSTOM:         dataType= "TYPE_CUSTOM";
  		case BufferedImage.TYPE_INT_ARGB:       dataType= " YPE_INT_ARGB";
  		case BufferedImage.TYPE_INT_ARGB_PRE:   dataType= "TYPE_INT_ARGB_PRE";
  		case BufferedImage.TYPE_INT_BGR:        dataType= "TYPE_INT_BGR";
  		case BufferedImage.TYPE_INT_RGB:        dataType= "TYPE_INT_RGB";
  		case BufferedImage.TYPE_USHORT_555_RGB: dataType= "TYPE_USHORT_555_RGB";
  		case BufferedImage.TYPE_USHORT_565_RGB: dataType= "TYPE_USHORT_565_RGB";
  		case BufferedImage.TYPE_USHORT_GRAY:    dataType= "TYPE_USHORT_GRAY";
	    default: dataType = "Unknown";
   }
	Board.appendTexln("Sample Model:      "+sm);
	Board.appendTexln("Data Type:         "+dataType);	
	Board.appendTexln("Bands:             "+sm.getNumBands());
	Board.appendTex  ("Data type:         ");
	switch(sm.getDataType()){
		case DataBuffer.TYPE_BYTE:      Board.appendTexln("Byte");      break;
		case DataBuffer.TYPE_SHORT:     Board.appendTexln("Short");     break;
		case DataBuffer.TYPE_USHORT:    Board.appendTexln("Ushort");    break;
		case DataBuffer.TYPE_INT:       Board.appendTexln("Int");       break;
		case DataBuffer.TYPE_FLOAT:     Board.appendTexln("Float");     break;
		case DataBuffer.TYPE_DOUBLE:    Board.appendTexln("Double");    break;
		case DataBuffer.TYPE_UNDEFINED: Board.appendTexln("Undefined"); break;
	    default: 						Board.appendTexln("Unknown");   break;
	}
	// Display info about the ColorModel of the image.
	ColorModel cm = pi.getColorModel();
	if (cm != null){
		Board.appendTexln("Color Model:       "+ cm);
		Board.appendTexln("Color components:  "+ cm.getNumComponents());
		Board.appendTexln("Bits per pixel:    "+cm.getPixelSize());
		Board.appendTex  ("Transparency:      ");
		switch(cm.getTransparency()){
			case Transparency.OPAQUE:      Board.appendTexln("opaque");      break;
			case Transparency.BITMASK:     Board.appendTexln("bitmask");     break;
			case Transparency.TRANSLUCENT: Board.appendTexln("translucent"); break;
		}
	}
	else Board.appendTexln("No color model");
	
	//optional additional info (uses ImageInfoUtil.java from book AWT 2D JAI Rodrigues)
//	String[][] info = ImageInfoUtil.getImageInfo(pi.getAsBufferedImage());
//	System.out.println("");
//	System.out.println("ImageInfo: Additional Info");
//	for (int i= 0; i< 10; i++){
//		System.out.println(""+info[i][0]+":             "+info[i][1]);
//	}
	

	}
}//END